#! /usr/bin/sdlbrt

finp$="picture.png"

setdisplay(256,192,32,1):paper(8^8-1):ink(0):pen(0):cls
loadimage(finp$,1):pasteicon(0,0,1)

dim npaletr[16],npaletg[16],npaletb[16]
dim realpal[16],graypl[16],celcnt[16]
dim opaletr[16],opaletg[16],opaletb[16]
dim xpnt[8],invrp[16],o4b[8]
dim ppalr[256],ppalg[256],ppalb[256]
dim bmapbf[8192],attrbf[8192]

realpal[ 0]= 1:realpal[ 1]= 4:realpal[ 2]= 6:realpal[ 3]=12
realpal[ 4]= 8:realpal[ 5]= 5:realpal[ 6]=13:realpal[ 7]= 2
realpal[ 8]= 9:realpal[ 9]= 7:realpal[10]=14:realpal[11]= 3
realpal[12]=11:realpal[13]=10:realpal[14]=15:realpal[15]=14

npaletb[ 0]=  0:npaletg[ 0]=  0:npaletr[ 0]=  0
npaletb[ 1]=  0:npaletg[ 1]=  0:npaletr[ 1]=  0
npaletb[ 2]= 36:npaletg[ 2]=219:npaletr[ 2]= 36
npaletb[ 3]=109:npaletg[ 3]=255:npaletr[ 3]=109
npaletb[ 4]=255:npaletg[ 4]= 36:npaletr[ 4]= 36
npaletb[ 5]=255:npaletg[ 5]=109:npaletr[ 5]= 73
npaletb[ 6]= 36:npaletg[ 6]= 36:npaletr[ 6]=182
npaletb[ 7]=255:npaletg[ 7]=219:npaletr[ 7]= 73
npaletb[ 8]= 36:npaletg[ 8]= 36:npaletr[ 8]=255
npaletb[ 9]=109:npaletg[ 9]=109:npaletr[ 9]=255
npaletb[10]= 36:npaletg[10]=219:npaletr[10]=219
npaletb[11]=146:npaletg[11]=219:npaletr[11]=219
npaletb[12]= 36:npaletg[12]=146:npaletr[12]= 36
npaletb[13]=182:npaletg[13]= 73:npaletr[13]=219
npaletb[14]=182:npaletg[14]=182:npaletr[14]=182
npaletb[15]=255:npaletg[15]=255:npaletr[15]=255

'---- define equivalente grayscale da paleta
for i=0 to 15
  graypl[i]=int(((npaletr[i]*30)+(npaletg[i]*59)+(npaletb[i]*11))/100)
  next

'---- criacao de realpal invertido
for i=0 to 15:invrp[realpal[i]]=i:next

'----main loop
for y1=0 to 23
  'locate 24,2:print using"###%";(y1*100)/23;
  setcaption(int((y1*100)/23))
  prbl=int((y1*50)/23)
  'locate 24,29:print mid$(prbar$,(65-prbl),50);

  for x1=0 to 31
    for y2=0 to 7

      '----atribute reading and mounting
      for i=0 to 15:celcnt[i]=0:next
      for x2=0 to 7
        y=(y1*8)+y2:xp=(x1*8)+x2:yi=191-y
        xh=int(xp/2)
        clq=point(xp,y)
        b=bitwiseand(255,clq):clq=int(clq/256)
        g=bitwiseand(255,clq):clq=int(clq/256)
        r=bitwiseand(255,clq)

        'b=point(xp,y)
        'g=point(xp,y)
        'r=point(xp,y)
        '---- getting msx colour space from rgb

        'tms9938
        'pindx= 9:                             pindx= 9
        ''g128,r128
        'if            g>=128            then: pindx= 7:end if
        'if r<128  and g<128             then: pindx= 1:end if
        'if r>=128 and g>=128            then: pindx=14:end if
        ''b128
        'if r<128  and g<128  and b>=128 then: pindx= 5:end if
        'if r>=128 and g<128  and b>=128 then: pindx=13:end if
        'if r<128  and g>=128 and b<128  then: pindx=12:end if
        'if r>=128 and g>=128 and b<128  then: pindx=10:end if
        ''g64
        'if r<128  and g<64   and b>=128 then: pindx= 4:end if
        'if r>=128 and g<64   and b<128  then: pindx= 6:end if
        'if r<128  and g>=192 and b<128  then: pindx= 3:end if
        'if r>=128 and g>=192 and b>=128 then: pindx=11:end if
        ''r64,b64
        'if r>=192 and g<64   and b<128  then: pindx= 8:end if
        'if r<64   and g>=192 and b<128  then: pindx= 2:end if
        'if r>=128 and g>=192 and b>=192 then: pindx=15:end if

        'tms9918
        pindx= 9:                             pindx= 9
        'g128,r128
        if            g>=128            then: pindx= 7:end if
        if r<128  and g<128             then: pindx= 1:end if
        if r>=128 and g>=128            then: pindx=14:end if
        'b128
        if r<128  and g<128  and b>=128 then: pindx= 5:end if
        if r>=128 and g<128  and b>=128 then: pindx=13:end if
        if r<128  and g>=128 and b<128  then: pindx=12:end if
        if r>=128 and g>=128 and b<128  then: pindx=10:end if
        'g64
        if r<128  and g<64   and b>=128 then: pindx= 4:end if
        if r>=128 and g<64   and b<128  then: pindx= 6:end if
        if r<128  and g>=192 and b<128  then: pindx= 3:end if
        if r>=128 and g>=192 and b>=128 then: pindx=11:end if
        'r64,b64
        if r>=192 and g<64   and b<128  then: pindx= 8:end if
        if r<64   and g>=192 and b<128  then: pindx= 2:end if
        if r>=128 and g>=210 and b>=210 then: pindx=15:end if



        '--------
        xpnt[x2]=pindx
        u=xpnt[x2]:celcnt[u]=celcnt[u]+1000+(rnd(99))
        next
      ikattr=0:paattr=0:ikcnt=0:pacnt=0
      for i=0 to 15
        if ikcnt<celcnt[i] then
          ikcnt=celcnt[i]:ikattr=i
          end if 
        next
      celcnt[ikattr]=0
      for i=0 to 15
        if pacnt<celcnt[i] then
          pacnt=celcnt[i]:paattr=i
          end if 
        next
      if ikattr<paattr then:swp=ikattr:ikattr=paattr:paattr=swp:end if
      lumd=(graypl[ikattr]+graypl[paattr])/2
      xyf=(x1*8)+y2+(y1*256)
      'seek #2,xyf+8192+8:lset fb2$=chr$((ikattr*16)+paattr):put #2
      attrbf[xyf]=(ikattr*16)+paattr

    '---- reading original 16 colour image as grayscale for screen 2 
    pkvar=0
    y=y2+(y1*8):yi=191-y
    for x2=0 to 7
      y=y2+(y1*8):x=x2+(x1*8):yi=191-y
      u=xpnt[x2]: ik=invrp[paattr]
      if graypl[u]>lumd then
        ik=invrp[ikattr]:pkvar=pkvar+(2^(7-x2))
        end if 
      o4b[x2]=bitwiseand(ik,15)
      next
    xyf=(x1*8)+y2+(y1*256)
    'seek #2,xyf+8:lset fb2$=chr$(pkvar):put #2
    bmapbf[xyf]=pkvar

    for x3=0 to 3
      x2=x3*2:x=x3+(x1*4)
      ik=(16*o4b[x2])+o4b[x2+1]
      'seek #3,119+(yi*128)+x:lset fb3$=chr$(ik):put #3 '- write bmp?
      next
    next
  next
next

open finp$+".bin" for output as #1
for i=0 to 6143
  writebyte (1,bmapbf[i])
  next
for i=0 to 6143
  writebyte (1,attrbf[i])
  next
close #1

'close #1:close #2:close #3
'grab(1,0,0,256,192):saveimage("pic1b.bmp",1)
'waitkey

